//
//  LCTySmartHomeTool.swift
//  Runner
//
//  Created by 林川 on 2021/1/14.
//

import UIKit
import TuyaSmartBaseKit
import TuyaSmartDeviceKit

class LCTySmartHomeTool: NSObject, FlutterStreamHandler {
    
    var channel:FlutterEventChannel?
    var events:FlutterEventSink?
    
    public override init() {
        super.init()
    }
    
    convenience init(messenger: FlutterBinaryMessenger) {
        self.init()
        
        channel = FlutterEventChannel(name: "ty_smart_home_event", binaryMessenger: messenger)
        channel?.setStreamHandler(self)
        
        //创建通知中心,设置监听的方法
        NotificationCenter.default.addObserver(self, selector: #selector(getConfigWIFIResult(noti:)), name:NSNotification.Name("getConfigWIFIResult"), object: nil)
        
        NotificationCenter.default.addObserver(self, selector: #selector(getTempHumi(noti:)), name:NSNotification.Name("getTempHumi"), object: nil)
        
    }
    
    
    func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
        self.events = events
        return nil
    }
    
    func onCancel(withArguments arguments: Any?) -> FlutterError? {
        self.events = nil
        return nil
    }
    
    @objc func getConfigWIFIResult(noti: NSNotification) {
//        print(noti)
        let result = noti.userInfo as! [String: String]
        if let e = events {
            e(result["result"])
        }
        
    }
    
    // 发送温度, 湿度信息
    @objc func getTempHumi(noti: NSNotification) {
//        print(noti)
        let result = noti.userInfo as! [String: String]
        if let e = events {
            e(result)
        }
    }
    
    deinit {
        /// 移除通知
//        NotificationCenter.default.removeObserver(self)
    }
    
}

